
const {
	HttpPost,URL,timestampToTime,getNowFormatDate
} = require('../../utils/utils');

const app = getApp()

Page({

  data: {
    todayRank:[],
    totalRank:[],

    loadDBRankOver:false,

    userInfo:null,

    currentTab: 1,
    winWidth: 0,
    winHeight: 0,

    globalData:null,

  },

  updateSelfCost:function(rankData){
    for (var i=0;i<this.data.todayRank.length;i++){
      if (this.data.todayRank[i]._openid == this.data.globalData.openId){
        this.data.todayRank[i].todayCost = this.data.globalData.todayCost
        this.data.todayRank[i].name = this.data.globalData.name
        break
      }
    }
    this.data.todayRank.sort(function(l,r) {
      return r.todayCost - l.todayCost
    })
    for (var i=0;i<this.data.totalRank.length;i++){
      if (this.data.totalRank[i]._openid == this.data.globalData.openId){
        this.data.totalRank[i].totalCost = this.data.globalData.totalCost
        this.data.totalRank[i].name = this.data.globalData.name
        break
      }
    }
    this.data.totalRank.sort(function(l,r) {
      return r.totalCost - l.totalCost
    })
    this.setData({todayRank:this.data.todayRank, totalRank:this.data.totalRank})
  },

  switchNav: function (e) {
   // console.log("switchNav", e)
    if (this.data.currentTab == e.target.dataset.current) {
      return false;
    }else {
      this.setData({ currentTab: e.target.dataset.current });
    }
  },
  bindChange: function (e) {
  //  console.log("bindChange", e)
    this.setData({
      currentTab: e.detail.current
    });
  },

  async onShow() {
    this.setData({globalData:app.globalData})   // 切换主题色生效

    if (this.data.loadDBRankOver){              // 后续切换tab，直接用自己的内存数据
      this.updateSelfCost()
    }
    if (this.data.globalData.changeTheme){
      this.data.globalData.changeTheme = false
      app.changeThemeImpl()
		}
    wx.showShareMenu({
      success(res) {
      },
      fail(e) {
      }
    });
    this.setData({globalData:app.globalData})
    await this.downloadCostRank()
  },

  async onLoad(options) {
  },

  async downloadCostRank(){
    var todayStr = getNowFormatDate()
    var that = this

    let res = await HttpPost('getallrecord',{
    })
    if (res.code != 0) {
        wx.showToast({
          title: '系统错误：' + res.msg,
        })
        return
		}
		let list = res.exercisedatalist || []
		var todaylist = [];
		for (const element of list) {
			if (element.today == todayStr){
				todaylist.push(element)
			}
		}
		todaylist.sort((a, b) => b.todaycost - a.todaycost)

		var totallist = list.concat();
		totallist.sort((a, b) => b.totalcost - a.totalcost)

    that.setData({todayRank:todaylist})
		that.setData({totalRank:totallist, loadDBRankOver:true})
		
		// console.log("downloadCostRank", todaylist, totallist, todaylist == totallist)
	},
	  // 分享到朋友圈的配置
		onShareTimeline() {
			return {
				// 分享的页面路径，可携带参数
				path: '/pages/mine/mine', 
			};
		},
		// 分享给好友的配置（可选）
		onShareAppMessage() {
			return {
				path: '/pages/mine/mine',
			};
		}
})